/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.userbill.service.impl;

import me.zhengjie.modules.userbill.domain.UserBill;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.userbill.repository.UserBillRepository;
import me.zhengjie.modules.userbill.service.UserBillService;
import me.zhengjie.modules.userbill.service.dto.UserBillDto;
import me.zhengjie.modules.userbill.service.dto.UserBillQueryCriteria;
import me.zhengjie.modules.userbill.service.mapstruct.UserBillMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author 毛晋辉
* @date 2020-09-02
**/
@Service
@RequiredArgsConstructor
public class UserBillServiceImpl implements UserBillService {

    private final UserBillRepository userBillRepository;
    private final UserBillMapper userBillMapper;

    @Override
    public Map<String,Object> queryAll(UserBillQueryCriteria criteria, Pageable pageable){
        Page<UserBill> page = userBillRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(userBillMapper::toDto));
    }

    @Override
    public List<UserBillDto> queryAll(UserBillQueryCriteria criteria){
        return userBillMapper.toDto(userBillRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public UserBillDto findById(Long id) {
        UserBill userBill = userBillRepository.findById(id).orElseGet(UserBill::new);
        ValidationUtil.isNull(userBill.getId(),"UserBill","id",id);
        return userBillMapper.toDto(userBill);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserBillDto create(UserBill resources) {
        return userBillMapper.toDto(userBillRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UserBill resources) {
        UserBill userBill = userBillRepository.findById(resources.getId()).orElseGet(UserBill::new);
        ValidationUtil.isNull( userBill.getId(),"UserBill","id",resources.getId());
        userBill.copy(resources);
        userBillRepository.save(userBill);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            userBillRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<UserBillDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (UserBillDto userBill : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", userBill.getUserId());
            map.put("关联id", userBill.getLinkId());
            map.put("0：支出；1：收入；", userBill.getPm());
            map.put("标题", userBill.getTitle());
            map.put("种类", userBill.getCategory());
            map.put("类型", userBill.getType());
            map.put("数字", userBill.getNumber());
            map.put("剩余金额", userBill.getBalance());
            map.put("备注", userBill.getMark());
            map.put("0:待确定；1：有效；-1无效", userBill.getStatus());
            map.put("创建时间", userBill.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}